<?php
//----------------------------------------------------------------------------//
// Class              : MD_Sanpham											  //
// Modification       :														  //
//----------------------------------------------------------------------------//
// Date(yyyy.mm.dd)   Developer                       Action				  //
//----------------------------------------------------------------------------//
// 2007.07.08         Oanhhtk                   Created					  //
//----------------------------------------------------------------------------//

class MD_Sanpham extends ModelBase{

	//==========================================================================
	// Function    :  MD_Sanpham
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.07.12
	// Details     : AppendData record of table
	//==========================================================================
	function MD_Sanpham(&$database){
		$this->tableName= TABLE_SAN_PHAM;
		$this->tableName_Cat_Sanpham=TABLE_LOAI_SAN_PHAM;
		$this->listFieldSearch  = "$this->tableName.*,$this->tableName_Cat_Sanpham.name as catsanpham";
	   	ModelBase::ModelBase($database);
	}
	//==========================================================================
	// Function    :  insert
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.07.08
	// Details     : AppendData record of table
	//==========================================================================
	function insert(&$arrayData){
	    //Thay dau ' thanh '' khi insert du lieu vao Database
		$ten_sp=$this->replaceString(&$arrayData["ten_sp"]);
		$mo_ta_sp= $this->replaceString(&$arrayData["mo_ta_sp"]);
		$chi_tiet_sp= $this->replaceString(&$arrayData["chi_tiet_sp"]);
		$ghi_chu=$this->replaceString(&$arrayData["ghi_chu"]);
		$dong_goi=$this->replaceString(&$arrayData["dong_goi"]);
		$hang_sx=$this->replaceString(&$arrayData["hang_sx"]);

		//Insert du lieu vao DB
		$listFilds .= "ten_sp,";	        $listValues  .= "'".$ten_sp."',";
		$listFilds .= "ghi_chu,";			$listValues  .= "'".$ghi_chu."',";
		$listFilds .= "trang_thai,";		$listValues  .= "'".$arrayData['trang_thai']."',";
		$listFilds .= "gia_sp,";		    $listValues  .= "'".$arrayData['gia_sp']."',";
		$listFilds .= "gia_sp_km,";		    $listValues  .= "'".$arrayData['gia_sp_km']."',";
		$listFilds .= "loai_tien,";		    $listValues  .= "'".$arrayData['loai_tien']."',";
		$listFilds .= "hang_sx,";			$listValues  .= "'".$hang_sx."',";
		$listFilds .= "bao_hanh,";			$listValues  .= "'".$arrayData['bao_hanh']."',";
		$listFilds .= "dong_goi,";			$listValues  .= "'".$dong_goi."',";
		$listFilds .= "thu_tu,";			$listValues  .= "'".$arrayData['thu_tu']."',";
		$listFilds .= "hien_thi,";		    $listValues  .= "'".$arrayData['hien_thi']."',";
		$listFilds .= "id_loai_sp,";	    $listValues  .= "".$arrayData["cat_id_add"].",";
		$listFilds .= "anh_sp,";		    $listValues  .= "'".$_FILES["userfile"]['name']."',";
		$listFilds .= "mo_ta_sp,";		    $listValues  .= "'".$mo_ta_sp."',";
		$listFilds .= "chi_tiet_sp,";		$listValues  .= "'".$chi_tiet_sp."',";
		$listFilds .= "ngay_dang";          $listValues  .= " now()";
		$sql = "INSERT INTO ".$this->tableName." (  $listFilds ) ";
		$sql .= " VALUES ($listValues ) ";
		//echo $sql;
		$this->db->query($sql);
	}
	//==========================================================================
	// Function    :  update
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.07.08
	// Details     : AppendData record of table
	//==========================================================================
	function update(&$arrayData)
	{
		//Thay dau ' thanh '' khi insert du lieu vao Database
		$ten_sp=$this->replaceString(&$arrayData["ten_sp"]);
		$mo_ta_sp= $this->replaceString(&$arrayData["mo_ta_sp"]);
		$chi_tiet_sp= $this->replaceString(&$arrayData["chi_tiet_sp"]);
		$ghi_chu=$this->replaceString(&$arrayData["ghi_chu"]);
		$dong_goi=$this->replaceString(&$arrayData["dong_goi"]);
		$hang_sx=$this->replaceString(&$arrayData["hang_sx"]);
		
		//Update du lieu vao DB
		$sql = "UPDATE  ".$this->tableName." SET ";
		$sql .= "ten_sp='".$ten_sp."',";
		$sql .= "mo_ta_sp='".$mo_ta_sp."'," ;
		$sql .= "chi_tiet_sp='".$chi_tiet_sp."'," ;
		$sql .= "ghi_chu='".$ghi_chu."'," ;
		$sql .= "trang_thai='".$arrayData['trang_thai']."',";
		$sql .= "gia_sp='".$arrayData['gia_sp']."',";
		$sql .= "gia_sp_km='".$arrayData['gia_sp_km']."',";
		$sql .= "id_loai_sp=".$arrayData["cat_id_add"] ."," ;
		$sql .= "loai_tien=".$arrayData["loai_tien"] ."," ;
		$sql .= "dong_goi='".$dong_goi."',";
		$sql .= "hang_sx='".$hang_sx."'," ;
		$sql .= "bao_hanh='".$arrayData['bao_hanh']."',";
		$sql .= "thu_tu='".$arrayData['thu_tu']."',";
		$sql .= "ngay_dang=now()," ;
		$sql .= "hien_thi=".$arrayData["hien_thi"] ."";
		if($_FILES["userfile"]['name']!=""){
			$sql .= ",anh_sp='".$_FILES["userfile"]['name']."'" ;
		}
		$sql .= " WHERE $this->keyField=".$arrayData["id"];
		
		
		//echo $sql;
		$this->db->query($sql);
	}
	function UpdateHomePage($arrayData,$id_loai_sp){
		
		$sql  = "UPDATE  ".$this->tableName." SET ";
		$sql .= "view_o_trang_chu = '1'";
		$sql .= " WHERE id in (".implode(",",$arrayData).") and id_loai_sp =".$id_loai_sp."";
		$this->db->query($sql);
		
	}
	
	function UpdateNotHomePage($arrayData,$id_loai_sp){
		
		$sql  = "UPDATE  ".$this->tableName." SET ";
		$sql .= "view_o_trang_chu = '0'";
		$sql .= " WHERE id not in (".implode(",",$arrayData).") and id_loai_sp=".$id_loai_sp."";
		$this->db->query($sql);
		
	}
	//==========================================================================
    // Function    : update_ht
    // Creator     : Thanh 2007.07.13
    // Details     : update a record of table
    //==========================================================================
    function update_ht($strID, $strHienThi){
		$hien_thi0 = "0"; 
		$hien_thi1 = "1"; 
        $sql = "UPDATE  ".$this->tableName." SET ";
        if ($strHienThi == 1){
             $sql .= "hien_thi='".$hien_thi0."'";                    
        } else {
             $sql .= "hien_thi='".$hien_thi1."'";                 
        }
        $sql .= " WHERE $this->keyField=".$strID;
        //echo $sql;
        $this->db->query($sql);
    }   
	//==========================================================================
    // Function    : update_ht
    // Creator     : Thanh 2007.07.13
    // Details     : update a record of table
    //==========================================================================
    function update_check($strID, $strViewhome){
		$viewhome0 = "0"; 
		$viewhome1 = "1"; 
        $sql = "UPDATE  ".$this->tableName." SET ";
        if ($strViewhome == 1){
             $sql .= "view_o_trang_chu='".$viewhome0."'";                    
        } else {
             $sql .= "view_o_trang_chu='".$viewhome1."'";                 
        }
        $sql .= " WHERE $this->keyField=".$strID;
        //echo $sql;
        $this->db->query($sql);
    }   
	//==========================================================================
	// Function    : getForSearchText
	// Creator     : ThanhTX 2007.06.08
	// Details     : Get all data from table
	// Update	   : Oanhhtk 2007.06.18  --> update using for admin	
	//==========================================================================
	
	function getForSearchText($ten_sp,$loai_sp,$listCatSP){
		
	    $sql = "SELECT * FROM ".$this->tableName."";
		$sql .= "  WHERE hien_thi = 1";		
		if(($ten_sp != "") && ($ten_sp != "Từ khóa")){
		$sql .= " and ten_sp like '%".$ten_sp."%'  ";
		}
		if($loai_sp != 0){
			if($listCatSP != ''){
			$sql .= " and id_loai_sp in (".$listCatSP.")";
			}else{
			$sql .= " and id_loai_sp =".$loai_sp."";
			}
		}
							
		$sql .= " order by ngay_dang desc limit 0,100";
		//echo $sql;
		//die();
		return $this->db->retrieve($sql);
	}
	
	
	function getForSearchProduct($tensp,$listCatSP,$id_loai,$id_hang){
		$sql  = "SELECT * FROM ".$this->tableName." ";
		$sql .= "WHERE ten_sp LIKE '%".$tensp."%' ";
		if($id_loai != 0){
			if($listCatSP != ''){
			$sql .= " and id_loai_sp in (".$listCatSP.")";
			}else{
			$sql .= " and id_loai_sp =".$id_loai."";
			}
		}
		if($id_hang != 0){
			$sql .= " and hang_sx ='".$id_hang."'";
		}
		$sql .= " order by ngay_dang desc";
		//echo $sql;
		//die();
		return $this->db->retrieve($sql);
	}
	//==========================================================================
	// Function    : getForSearchAdvanceText
	// Creator     : ThanhTX 2007.06.08
	// Details     : Get all data from table
	// Update	   : Oanhhtk 2007.06.18  --> update using for admin	
	//==========================================================================
	function getForSearchAdvanceText($nameTitle,$loaisp,$sanpham,$price_from,$price_to,$hangsx,$listCatSP){
		$sql = "SELECT * FROM ".$this->tableName."";
		$sql .= "  WHERE hien_thi = 1 ";
		if($nameTitle != ''){
		$sql .= " and ten_sp like '%".$nameTitle."%'  ";
		}
		if($loaisp != 0){
			if($listCatSP != ''){
			$sql .= " and id_loai_sp in (".$listCatSP.")";
			}else{
			$sql .= " and id_loai_sp =".$loaisp."";
			}
		}
		
		if(($sanpham != 0) && ($price_from != '') && ($price_to != '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km>= '".$price_from."' and gia_sp_km <= '".$price_to."'";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp>= '".$price_from."' and gia_sp <= '".$price_to."' and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and gia_sp>= '".$price_from."' and gia_sp <= '".$price_to."' and solanban > 0";
			}
		}elseif(($sanpham == 0) && ($price_from != '') && ($price_to != '')){
			$sql .= " and (gia_sp >= '".$price_from."') and (gia_sp <= '".$price_to."')";
		}elseif(($sanpham == 0) && ($price_from != '') && ($price_to == '')){
			$sql .= " and gia_sp >= '".$price_from."'";
		}elseif(($sanpham == 0) && ($price_from == '') && ($price_to != '')){
			$sql .= " and gia_sp = '".$price_to."'";
		}elseif(($sanpham != 0) && ($price_from != '') && ($price_to == '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km = '".$price_from."'";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp >= '".$price_from."' and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and gia_sp >= '".$price_from."' and solanban > 0";
			}
		}elseif(($sanpham != 0) && ($price_from == '') && ($price_to != '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km = '".$price_to."'";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp = '".$price_to."' and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and gia_sp = '".$price_to."' and solanban > 0";
			}
		}elseif(($sanpham != 0) && ($price_from == '') && ($price_to == '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km > 0";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and solanban > 0";
			}
		}
		
		if($hangsx != 0){
			$sql .= " and hang_sx ='".$hangsx."'";
		}
		
		$sql .= " order by ngay_dang desc limit 0,100";
		//echo $sql;
		//die();
		return $this->db->retrieve($sql);
	}
	//==========================================================================
    // Function    : getContentNews("id,tieu_de",NUM_NEWREC);
    // Creator     : Thanhtx 2007.07.12
    // Details     : AppendData record of table
    //==========================================================================
    function getContentNews($strFields,$intNum_NewRec){
        if ($strFields != ""){
            $sql = "SELECT  ".$strFields." FROM ".$this->tableName." WHERE shows = 1 and section_id=1  OR section_id=3 ORDER BY id desc LIMIT $intNum_NewRec;";
        } else {
            $sql = "SELECT * FROM ".$this->tableName." WHERE shows = 1 and section_id=1  OR  section_id=3 ORDER BY id desc LIMIT $intNum_NewRec;";
        }
        //echo $sql;
        return $this->db->retrieve($sql);
    }
	//==========================================================================
    // Function    : getContentNew("id,tieu_de",NUM_NEWREC);
    // Creator     : Thanhtx 2007.07.12
    // Details     : AppendData record of table
    //==========================================================================
    function getContentNew($strFields,$intNum_NewRec){
        if ($strFields != ""){
            $sql = "SELECT  ".$strFields." FROM ".$this->tableName." WHERE shows = 1 and section_id=3 ORDER BY id desc LIMIT $intNum_NewRec;";
        } else {
            $sql = "SELECT * FROM ".$this->tableName." WHERE shows = 1 and section_id=3 ORDER BY id desc LIMIT $intNum_NewRec;";
        }
        //echo $sql;
        return $this->db->retrieve($sql);
    }
	//==========================================================================
    // Function    : getContentFocus("id,tieu_de",NUM_NEWREC);
    // Creator     : Thanhtx 2007.07.12
    // Details     : AppendData record of table
    //==========================================================================
    function getContentFocus($strFields,$intNum_NewRec){
        if ($strFields != ""){
            $sql = "SELECT  ".$strFields." FROM ".$this->tableName." WHERE shows = 1 and section_id=2 ORDER BY id desc LIMIT $intNum_NewRec;";
        } else {
            $sql = "SELECT * FROM ".$this->tableName." WHERE shows = 1 and section_id=3 ORDER BY id desc LIMIT $intNum_NewRec;";
        }
        //echo $sql;
        return $this->db->retrieve($sql);
    }
	//==========================================================================
	// Function    :  update
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.07.12
	// Details     : AppendData record of table
	//==========================================================================
	function updateCountClick(&$id)
	{
		$sql = "UPDATE  ".$this->tableName." SET ";
		$sql .= "hits=hits+1";
		$sql .= " WHERE $this->keyField=".$id;
		$this->db->query($sql);
	}
	//==========================================================================
	// Function    :  updateanh
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.03.08
	// Details     : AppendData record of table
	//==========================================================================
	function updateanh(&$arrayData){
		$sql = "UPDATE  ".$this->tableName." SET ";
		$sql .= "anh_sp=''" ;	
		$sql .= " WHERE $this->keyField=".$arrayData["id"];
		$this->db->query($sql);
	}
	//==========================================================================
	// Function    : getFullById
	// Argument    : 
	// Creator     : ThanhTX 2007.06.11
	// Details     : Get record with id
	//==========================================================================
	function getFullById($id){
		$sql = "SELECT * FROM $this->tableName ";
		$sql .= "  WHERE id=$id";
		//echo $sql;
		return $this->db->getFirstRecord($sql);
	}
	//==========================================================================
	// Function    : getByWhereLimit20
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.03.09
	// Details     : AppendData record of table
	//==========================================================================
	function getByWhereLimit20($listField,$where="",$order=""){
		if ($where==""){
			$sql = "SELECT $listField  FROM $this->tableName  ";
		}else{
  			$sql = "SELECT $listField  FROM $this->tableName  WHERE $where ";
		}
		if ($order !=""){
			$sql .= " Order by $order";
		}
		if ($order !=""){
			$sql .= " limit 1,20";
		}
		//global $db;
		return $this->db->retrieve($sql);
	}
	//==========================================================================
	// Function    : getForSearchTextAdmin
	// Creator     : ThanhTX 2007.06.08
	// Details     : Get all data from table
	// Update	   : Oanhhtk 2007.06.18  --> update using for admin	
	//==========================================================================
	function getForSearchTextAdmin($searchText,$cat_id,$section_id,$shows){
		$sql = "SELECT $this->tableName.* FROM $this->tableName";
		$sql .= "  WHERE ";
		$sql .= "(title like '%".$searchText."%' or ";
		$sql .= "introtext like '%".$searchText."%' or ";
		$sql .= "fulltexts like '%".$searchText."%') and ";
		$sql .= "(1=1 ";
		if($cat_id!=""){
			$sql .= "and cat_id =".$cat_id."  ";
		}
		if($section_id!=""){
			$sql .= "and section_id =".$section_id." ";
		}
		if($shows!=""){
			$sql .= "and shows=".$shows." ";
		}
		$sql .= ")";
		$sql .= " order by $this->tableName.id desc";
		//echo $sql;
		return $this->db->retrieve($sql);
	}
}
?>